#include "fxcc/core/graph/opengl3/ImGuiGlfwApp.h"
#include "fxcc/core/graph/common/Geometry.h"
#include "fxcc/core/graph/opengl3/ssao/Effect.h"
#include "fxcc/core/graph/opengl3/ssao/GBufferEffect.h"

#include "fxcc/imgui/Editor.h"

using namespace fxcc::platform;
using namespace fxcc::graph;
using namespace fxcc::graph::opengl3;
using namespace fxcc::imgui::graph::opengl3;
using BaseApp = fxcc::imgui::graph::opengl3::GlfwApp;

#undef main

struct SimpleApp : public BaseApp
{
public:
    fxcc::graph::common::Transform transform;

    std::shared_ptr<fxcc::graph::opengl3::RenderTarget> m_RenderTarget;
    std::shared_ptr<fxcc::graph::opengl3::Mesh> m_CubeMesh;
    std::shared_ptr<fxcc::graph::opengl3::ssao::Effect> m_SSAOEffect;
    std::shared_ptr<fxcc::graph::opengl3::ssao::GBufferEffect> m_GBufferEffect;

    std::shared_ptr<fxcc::graph::opengl3::Effect3d> m_Effect;

    glm::vec4 color;
    glm::vec4 objColor{1.0f};

    fxcc::graph::common::Camera camera;

    SimpleApp(const fxcc::platform::common::App::Desc &desc) : BaseApp(desc), color(1.0)
    {
    }
    bool Init() override
    {
        if (!BaseApp::Init())
        {
            return false;
        }

        if (!InitResources())
        {
            return false;
        }

        return true;
    };

    void printOpenGLESVersion()
    {
        const char *version = (const char *)glGetString(GL_VERSION);
        std::cout << "OpenGL ES Version: " << version << std::endl;
    };


    bool InitResources()
    {

        camera.SetPosition(glm::vec3(5.0f));
        camera.LookAt(glm::vec3(0.0f));
        camera.nNear = 1.0f;
        camera.nFar = 50.0f;

        auto geoData = fxcc::graph::common::Geometry::CreateBox();
        fxcc::graph::common::MeshDesc meshDesc(geoData);
        m_CubeMesh = std::make_shared<fxcc::graph::opengl3::Mesh>(meshDesc);

        m_GBufferEffect = std::make_shared<fxcc::graph::opengl3::ssao::GBufferEffect>();

        std::string fragCode = R"(

out vec4 FragColor;
uniform vec4 color;
void main()
{
    FragColor=color;

}
)";
        m_Effect = std::make_shared<fxcc::graph::opengl3::Effect3d>(fragCode);
        return true;
    };

    fxcc::imgui::Editor editor;

    void OnEdit() override
    {

        ImGui::Begin("Hello world");
        //editor.ImageEdit<fxcc::graph::opengl3::RenderTarget>("rt1", *m_RenderTarget, ImVec2(400, 300));
        
        editor.EditInst<glm::vec4>("objColor", objColor);

        ImGui::End();
    }
    void OnDestroy() override
    {
      
        BaseApp::OnDestroy();
    }
    void OnFrameRender() override
    {
        glViewport(0, 0, m_Desc.m_Size.x, m_Desc.m_Size.y);

        glClearColor(color.x, color.y, color.z, color.w);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


        m_Effect->Begin();
        m_Effect->SetCamera(camera);
        
        m_Effect->m_PipeLineType.m_RasterizerType = fxcc::graph::common::RasterizerType::_NONE_;
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

        m_Effect->m_Shader->setVec4("objColor", objColor);
        m_Effect->InstMesh(m_CubeMesh.get(), transform);
        m_Effect->End();


        

    };

    void OnFramebuffersize(int w, int h) override
    {
        opengl3::GlfwApp::OnFramebuffersize(w, h);
        camera.SetAspect(m_Desc.Aspect());

        {
            fxcc::graph::common::RenderTargetDesc desc;
            
            desc.InitDesc2(6, w, h, 3, false);
            m_RenderTarget = std::make_shared<fxcc::graph::opengl3::RenderTarget>(desc);

        }

    }
    void OnUpdate() override
    {
        opengl3::GlfwApp::OnUpdate();

        float ctTime = m_GlfwlTimer.m_CurrentTime;
        camera.FreeMove(m_Input, m_GlfwlTimer.m_DeltaTime);

        float deltaTime = m_GlfwlTimer.m_DeltaTime;

       


    }
};

int main()
{
    ztclog::pushFunction([](const ztclog::Line &line)
                         {
		if (line.m_Type != ztclog::Line::Type::_line_debug_)
		{
			std::cout << line.m_Text << std::endl;
		} });

    using _App = SimpleApp;
    using _Desc = fxcc::platform::common::App::Desc;

    _Desc desc;
    desc.m_Pos = glm::ivec2(100, 100);
    desc.m_Size = glm::ivec2(900, 600);
    desc.m_ScaleFactor = 1.0f;

    desc.m_Title = "test font";
    std::shared_ptr<_App> _app = std::make_shared<_App>(desc);
    return _app->Run();
};
